#include "runshopgoodsmanager.h"
#include "tlbbcode.h"
#include "QDebug"

using namespace cv;

Q_GLOBAL_STATIC(RunShopGoodsManager, instance)
RunShopGoodsManager::RunShopGoodsManager()
{
    times << "子" << "丑" << "寅"<< "卯" << "辰" << "巳"<< "午"<< "未"<< "申" << "酉"<< "戌" <<"亥";
    goodsNames << "粮食"<< "扎染" << "普洱"  << "干花";
    runShopGoods->append(new RunShopGoods(goodsNames[0], ":/runshop/icon/runshop/runshop_liang.png", ":/runshop/icon/runshop/runshop_liang2.png", Scalar(0, 61, 90), Scalar(11, 161, 251),"94 50", "2 29 63", ":/runshop/icon/runshop/noliangshi.png",Scalar(0, 204, 70), Scalar(17, 220, 189)));
//    runShopGoods->append(new RunShopGoods(goodsNames[1], ":/runshop/icon/runshop/runshop_dami.png", ":/runshop/icon/runshop/runshop_dami2.png", Scalar(21, 0, 109), Scalar(28, 160, 255)));
    runShopGoods->append(new RunShopGoods(goodsNames[1], ":/runshop/icon/runshop/runshop_zaran.png", ":/runshop/icon/runshop/runshop_zaran2.png", Scalar(132, 167, 45), Scalar(149, 193, 121), "2 36 25", "5 74 6", ":/runshop/icon/runshop/noliangshi.png",Scalar(0, 204, 70), Scalar(17, 220, 189)));
    runShopGoods->append(new RunShopGoods(goodsNames[2], ":/runshop/icon/runshop/runshop_puer.png", ":/runshop/icon/runshop/runshop_puer2.png", Scalar(0, 97, 77), Scalar(7, 142, 122), "1 25 0", "2 61 24", ":/runshop/icon/runshop/noliangshi.png",Scalar(0, 204, 70), Scalar(17, 220, 189)));
    runShopGoods->append(new RunShopGoods(goodsNames[3], ":/runshop/icon/runshop/runshop_ganhua.png", ":/runshop/icon/runshop/runshop_ganhua2.png",Scalar(25, 90, 120), Scalar(42, 163, 255), "1 69 0", "4 59 26", ":/runshop/icon/runshop/noliangshi.png",Scalar(0, 204, 70), Scalar(17, 220, 189)));


    xiaoWeiLocations << "无"<<"正东校尉" << "东南校尉" << "正南校尉" << "西南校尉" << "正西校尉" << "西北校尉" << "正北校尉" << "东北校尉";
    xiaoWeiLocationsImgs << "" << ":/runshop/icon/runshop/xiaowei_zhengdong.png" << ":/runshop/icon/runshop/xiaowei_dongnan.png"
                          <<":/runshop/icon/runshop/xiaowei_zhengnan.png"<< ":/runshop/icon/runshop/xiaowei_xinan.png"
                         <<":/runshop/icon/runshop/xiaowei_zhengxi.png" << ":/runshop/icon/runshop/xiaowei_xibei.png"
                        <<":/runshop/icon/runshop/xiaowei_zhengbei.png" << ":/runshop/icon/runshop/xiaowei_dongbei.png";
}

RunShopGoodsManager::~RunShopGoodsManager()
{
    if(runShopGoods != nullptr){
        qDeleteAll(runShopGoods->begin(), runShopGoods->end());
        runShopGoods->clear();
        delete runShopGoods;
        runShopGoods = nullptr;
    }
    qDebug() << "RunShopGoodsManager析构";
}

RunShopGoodsManager *RunShopGoodsManager::getInstance()
{
    return instance();
}

QStringList RunShopGoodsManager::getTimes(){
    return times;
}

QStringList RunShopGoodsManager::getBestTime(int timeCode)
{
    QStringList bestTimes;
    if(timeCode == GROUP2ERROR) return bestTimes;
    bestTimes << times.at(timeCode);
    for(int i = 0 ; i < 3; i++){
        for(int j = 0 ; j < 3;j++){
            timeCode++;
            if(timeCode > SHICHEN_HAI){
                timeCode = SHICHEN_ZI;
            }
        }

        bestTimes << times.at(timeCode);
    }
    return bestTimes;
}

QStringList RunShopGoodsManager::getXiaoWeiLocations()
{
    return xiaoWeiLocations;
}

QString RunShopGoodsManager::getXiaoWeiLocationsImgs(QString xiaoWeiLocation)
{
    int index = xiaoWeiLocations.indexOf(xiaoWeiLocation);
    if(index == -1) qDebug() << "未找到" << xiaoWeiLocation << "对应的图片模板";
    return xiaoWeiLocationsImgs.at(index);
}

RunShopGoods* RunShopGoodsManager::getGoods(QString name)
{
    for(int i = 0; i < runShopGoods->size(); i++){
        RunShopGoods* goods = runShopGoods->at(i);
        if(goods->name == name){
            return goods;
        }
    }
}

QStringList RunShopGoodsManager::getGoodsNames()
{
    return goodsNames;
}

int RunShopGoodsManager::index(QString name)
{
    return goodsNames.indexOf(name);
}

//void RunShopGoodsManager::setGoodsTime(QString goodsName, int time)
//{
//    int index = goodsNames.indexOf(goodsName);
//    if(index != -1){
//       QList<int> times = calculateTime(time);
//       RunShopGoods *runshopGoods = runShopGoods->at(index);
//       for(int i = 0 ; i < 4; i ++){
//            runshopGoods->time[i] = times[i];
//       }
//    }
//}

QList<int> RunShopGoodsManager::calculateTime(int time)
{
    QList<int> times;
    times<<time;
    for(int i = 0 ; i < 3; i++){
        for(int j = 0 ; j < 3;j++){
            time++;
            if(time > SHICHEN_HAI){
                time = SHICHEN_ZI;
            }
        }

        times << time;
    }
    return times;
}
